package cn.ruituo.service;

import cn.ruituo.dao.StudentDao;
import cn.ruituo.vo.Page;
import cn.ruituo.entity.Student;
import cn.ruituo.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
//业务逻辑层
@Service
@Transactional(readOnly = true)
//@Scope("singleton")
@Cacheable//允许缓存
@CacheConfig(cacheNames = "student",keyGenerator = "keyg")
public class StudentService {

    @Autowired
    private StudentDao dao;

    public Page<Student> paging(int pageNo, int pageSize, String sname, String qq, String telephone, Date enterDateStart, Date enterDateEnd) {

        List<Student> list = dao.paging(pageNo, pageSize, sname, qq, telephone, enterDateStart, enterDateEnd);
        int total = dao.pageTotal(sname, qq, telephone, enterDateStart, enterDateEnd);
        return new Page<Student>(list, pageNo, total,pageSize);

    }

    public Page<Student> paging(int pageNo, String sname, String qq, String telephone, Date enterDateStart, Date enterDateEnd) {

        List<Student> list = dao.paging(pageNo, Page.PAGE_SIZE, sname, qq, telephone, enterDateStart, enterDateEnd);
        int total = dao.pageTotal(sname, qq, telephone, enterDateStart, enterDateEnd);
        return new Page<Student>(list, total, pageNo);

    }

    @Transactional(readOnly = false)
    @CacheEvict(allEntries = true)//清除缓存
    public int add(Student stu) {

        return dao.add(stu);

    }

    //修改信息，考试成绩涨一份
    @Transactional(readOnly = false)
    @CacheEvict(allEntries = true)//清除缓存
    public int update(Student stu) {
        //dao.increaseScore(stu.getStuno(),1);
        int r = dao.update(stu);

        return r;
    }

    public Student get(long stuno) {

        return dao.get(stuno);

    }

    //验证手机号是否重复
    public boolean checkPhone(String phone) {

        return dao.checkPhone(phone) != null;

    }
    @Transactional(readOnly = false)
    @CacheEvict(allEntries = true)//清除缓存
    public int del(long stuno) {
        return dao.del(new long[]{stuno});
    }
}
